package com.yueke.gemini.file;

import java.io.File;
import java.util.*;
import java.util.Map.Entry;

/**
 * 统计文件个数
 *
 * @author Administrator
 *
 */
public class FileCountTest {

    // ??????resultMap,???????????
    public static final Map<String, Integer> resultMap = new HashMap<String, Integer>();

    public static void main(String[] args) {
        System.out.println("input path : ");
        String path = new Scanner(System.in).nextLine();
        getFileType(path);

        // ??map??key?????map
        Set<Entry<String, Integer>> entrySet = resultMap.entrySet();
        Iterator<Entry<String, Integer>> it = entrySet.iterator();
        while (it.hasNext()) {
            Entry<String, Integer> entry = it.next();
            //System.out.println(entry.getKey() + "=" + entry.getValue());
            System.out.println(entry.getKey());
        }
    }


    /**
     * ???????????????
     * @param path ????
     */
    public static void getFileType(String path) {
        File file = new File(path);

        // ??path??????
        if (!file.exists()) {
            return ;
        }

        // ??path???????????????
        File[] fileList = file.listFiles();
        for (File fileTemp : fileList) {

            // ????File???
            if (fileTemp.isFile()) {

                // ???????
                String endTemp = fileTemp.getName().substring(fileTemp.getName().lastIndexOf(".") + 1);
                Integer num = resultMap.get(endTemp);
                if (num == null) {
                    resultMap.put(endTemp, 1);
                } else {
                    resultMap.put(endTemp, num + 1);
                }
            }

            // ????File???
            if (fileTemp.isDirectory()) {

                // ????getFileType()??
                String pathTemp = fileTemp.getAbsolutePath();
                getFileType(pathTemp);
            }
        }
    }
}
